<?php
define("EW_PAGE_ID", "add", TRUE); // Page ID
define("EW_TABLE_NAME", 'users', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php include "userfn50.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('users');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanAdd()) {
	$Security->SaveLastUrl();
	Page_Terminate("userslist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$users->Export = @$_GET["export"]; // Get export parameter
$sExport = $users->Export; // Get export parameter, used in header
$sExportFile = $users->TableVar; // Get export file, used in header
?>
<?php

// Load key values from QueryString
$bCopy = TRUE;
if (@$_GET["id"] != "") {
  $users->id->setQueryStringValue($_GET["id"]);
} else {
  $bCopy = FALSE;
}

// Create form object
$objForm = new cFormObj();

// Process form if post back
if (@$_POST["a_add"] <> "") {
  $users->CurrentAction = $_POST["a_add"]; // Get form action
  LoadFormValues(); // Load form values
} else { // Not post back
  if ($bCopy) {
    $users->CurrentAction = "C"; // Copy Record
  } else {
    $users->CurrentAction = "I"; // Display Blank Record
    LoadDefaultValues(); // Load default values
  }
}

// Perform action based on action code
switch ($users->CurrentAction) {
  case "I": // Blank record, no action required
		break;
  case "C": // Copy an existing record
   if (!LoadRow()) { // Load record based on key
      $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
      Page_Terminate($users->getReturnUrl()); // Clean up and return
    }
		break;
  case "A": // ' Add new record
		$users->SendEmail = TRUE; // Send email on add success
    if (AddRow()) { // Add successful
      $_SESSION[EW_SESSION_MESSAGE] = "Add New Record Successful"; // Set up success message
      Page_Terminate($users->KeyUrl($users->getReturnUrl())); // Clean up and return
    } else {
      RestoreFormValues(); // Add failed, restore form values
    }
}

// Render row based on row type
$users->RowType = EW_ROWTYPE_ADD;  // Render add type
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "add"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_user_level"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Level"))
				return false;
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript">
<!--
var ew_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Add to TABLE: users<br><br><a href="<?php echo $users->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Mesasge in Session, display
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
  $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
}
?>
<form name="fusersadd" id="fusersadd" action="usersadd.php" method="post" onSubmit="return ew_ValidateForm(this);">
<p>
<input type="hidden" name="a_add" id="a_add" value="A">
<table class="ewTable">
  <tr class="ewTableRow">
    <td class="ewTableHeader">Full Name</td>
    <td<?php echo $users->fullname->CellAttributes() ?>><span id="cb_x_fullname">
<input type="text" name="x_fullname" id="x_fullname"  size="30" maxlength="100" value="<?php echo $users->fullname->EditValue ?>"<?php echo $users->fullname->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">User Name</td>
    <td<?php echo $users->user_name->CellAttributes() ?>><span id="cb_x_user_name">
<input type="text" name="x_user_name" id="x_user_name"  size="30" maxlength="50" value="<?php echo $users->user_name->EditValue ?>"<?php echo $users->user_name->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Password</td>
    <td<?php echo $users->user_pass->CellAttributes() ?>><span id="cb_x_user_pass">
<input type="password" name="x_user_pass" id="x_user_pass" value="<?php echo $users->user_pass->EditValue ?>" size="30" maxlength="50"<?php echo $users->user_pass->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">Email</td>
    <td<?php echo $users->email->CellAttributes() ?>><span id="cb_x_email">
<input type="text" name="x_email" id="x_email"  size="30" maxlength="100" value="<?php echo $users->email->EditValue ?>"<?php echo $users->email->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Address</td>
    <td<?php echo $users->address->CellAttributes() ?>><span id="cb_x_address">
<input type="text" name="x_address" id="x_address"  size="30" maxlength="200" value="<?php echo $users->address->EditValue ?>"<?php echo $users->address->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">Handphone</td>
    <td<?php echo $users->user_handphone->CellAttributes() ?>><span id="cb_x_user_handphone">
<input type="text" name="x_user_handphone" id="x_user_handphone"  size="30" maxlength="20" value="<?php echo $users->user_handphone->EditValue ?>"<?php echo $users->user_handphone->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableRow">
    <td class="ewTableHeader">Homephone</td>
    <td<?php echo $users->user_homephone->CellAttributes() ?>><span id="cb_x_user_homephone">
<input type="text" name="x_user_homephone" id="x_user_homephone"  size="30" maxlength="20" value="<?php echo $users->user_homephone->EditValue ?>"<?php echo $users->user_homephone->EditAttributes() ?>>
</span></td>
  </tr>
  <tr class="ewTableAltRow">
    <td class="ewTableHeader">Level<span class='ewmsg'>&nbsp;*</span></td>
    <td<?php echo $users->user_level->CellAttributes() ?>><span id="cb_x_user_level">
<?php if (!$Security->IsAdmin() && $Security->IsLoggedIn()) { // Non system admin ?>
<div<?php echo $users->user_level->ViewAttributes() ?>><?php echo $users->user_level->EditValue ?></div>
<?php } else { ?>
<select id="x_user_level" name="x_user_level"<?php echo $users->user_level->EditAttributes() ?>>
<!--option value="">Please Select</option-->
<?php
if (is_array($users->user_level->EditValue)) {
	$arwrk = $users->user_level->EditValue;
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($users->user_level->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected" : "";	
?>
<option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
</option>
<?php
			}
}
?>
</select>
<?php } ?>
</span></td>
  </tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="    Add    ">
</form>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load default values
function LoadDefaultValues() {
	global $users;
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $users;
	$users->fullname->setFormValue($objForm->GetValue("x_fullname"));
	$users->user_name->setFormValue($objForm->GetValue("x_user_name"));
	$users->user_pass->setFormValue($objForm->GetValue("x_user_pass"));
	$users->email->setFormValue($objForm->GetValue("x_email"));
	$users->address->setFormValue($objForm->GetValue("x_address"));
	$users->user_handphone->setFormValue($objForm->GetValue("x_user_handphone"));
	$users->user_homephone->setFormValue($objForm->GetValue("x_user_homephone"));
	$users->user_level->setFormValue($objForm->GetValue("x_user_level"));
}

// Restore form values
function RestoreFormValues() {
	global $users;
	$users->fullname->CurrentValue = $users->fullname->FormValue;
	$users->user_name->CurrentValue = $users->user_name->FormValue;
	$users->user_pass->CurrentValue = $users->user_pass->FormValue;
	$users->email->CurrentValue = $users->email->FormValue;
	$users->address->CurrentValue = $users->address->FormValue;
	$users->user_handphone->CurrentValue = $users->user_handphone->FormValue;
	$users->user_homephone->CurrentValue = $users->user_homephone->FormValue;
	$users->user_level->CurrentValue = $users->user_level->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $users;
	$sFilter = $users->SqlKeyFilter();
	if (!is_numeric($users->id->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@id@", ew_AdjustSql($users->id->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$users->Row_Selecting($sFilter);

	// Load sql based on filter
	$users->CurrentFilter = $sFilter;
	$sSql = $users->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$users->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $users;
	$users->id->setDbValue($rs->fields('id'));
	$users->fullname->setDbValue($rs->fields('fullname'));
	$users->user_name->setDbValue($rs->fields('user_name'));
	$users->user_pass->setDbValue($rs->fields('user_pass'));
	$users->email->setDbValue($rs->fields('email'));
	$users->address->setDbValue($rs->fields('address'));
	$users->user_handphone->setDbValue($rs->fields('user_handphone'));
	$users->user_homephone->setDbValue($rs->fields('user_homephone'));
	$users->user_level->setDbValue($rs->fields('user_level'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $users;

	// Call Row Rendering event
	$users->Row_Rendering();

	// Common render codes for all row types
	// fullname

	$users->fullname->CellCssStyle = "";
	$users->fullname->CellCssClass = "";

	// user_name
	$users->user_name->CellCssStyle = "";
	$users->user_name->CellCssClass = "";

	// user_pass
	$users->user_pass->CellCssStyle = "";
	$users->user_pass->CellCssClass = "";

	// email
	$users->email->CellCssStyle = "";
	$users->email->CellCssClass = "";

	// address
	$users->address->CellCssStyle = "";
	$users->address->CellCssClass = "";

	// user_handphone
	$users->user_handphone->CellCssStyle = "";
	$users->user_handphone->CellCssClass = "";

	// user_homephone
	$users->user_homephone->CellCssStyle = "";
	$users->user_homephone->CellCssClass = "";

	// user_level
	$users->user_level->CellCssStyle = "";
	$users->user_level->CellCssClass = "";
	if ($users->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($users->RowType == EW_ROWTYPE_ADD) { // Add row

		// fullname
		$users->fullname->EditCustomAttributes = "";
		$users->fullname->EditValue = ew_HtmlEncode($users->fullname->CurrentValue);

		// user_name
		$users->user_name->EditCustomAttributes = "";
		$users->user_name->EditValue = ew_HtmlEncode($users->user_name->CurrentValue);

		// user_pass
		$users->user_pass->EditCustomAttributes = "";
		$users->user_pass->EditValue = ew_HtmlEncode($users->user_pass->CurrentValue);

		// email
		$users->email->EditCustomAttributes = "";
		$users->email->EditValue = ew_HtmlEncode($users->email->CurrentValue);

		// address
		$users->address->EditCustomAttributes = "";
		$users->address->EditValue = ew_HtmlEncode($users->address->CurrentValue);

		// user_handphone
		$users->user_handphone->EditCustomAttributes = "";
		$users->user_handphone->EditValue = ew_HtmlEncode($users->user_handphone->CurrentValue);

		// user_homephone
		$users->user_homephone->EditCustomAttributes = "";
		$users->user_homephone->EditValue = ew_HtmlEncode($users->user_homephone->CurrentValue);

		// user_level
		$users->user_level->EditCustomAttributes = "";
		if (!$Security->CanAdmin()) { // System admin
			$users->user_level->EditValue = "********";
		} else {
		$sSqlWrk = "SELECT `UserLevelID`, `UserLevelName` FROM `userlevels`";
		$rswrk = $conn->Execute($sSqlWrk);
		$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
		if ($rswrk) $rswrk->Close();
		array_unshift($arwrk, array("", "Please Select"));
		$users->user_level->EditValue = $arwrk;
		}
	} elseif ($users->RowType == EW_ROWTYPE_EDIT) { // Edit row
	} elseif ($users->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$users->Row_Rendered();
}
?>
<?php

// Add record
function AddRow() {
	global $conn, $Security, $users;

	// Check for duplicate key
	$bCheckKey = TRUE;
	$sFilter = $users->SqlKeyFilter();
	if (trim(strval($users->id->CurrentValue)) == "") {
		$bCheckKey = FALSE;
	} else {
		$sFilter = str_replace("@id@", ew_AdjustSql($users->id->CurrentValue), $sFilter); // Replace key value
	}
	if (!is_numeric($users->id->CurrentValue)) {
		$bCheckKey = FALSE;
	}
	if ($bCheckKey) {
		$rsChk = $users->LoadRs($sFilter);
		if ($rsChk && !$rsChk->EOF) {
			$_SESSION[EW_SESSION_MESSAGE] = "Duplicate value for primary key";
			$rsChk->Close();
			return FALSE;
		}
	}
	if ($users->user_pass->CurrentValue <> "") { // Check field with unique index
		$sFilter = "(`user_pass` = '" . ew_AdjustSql($users->user_pass->CurrentValue) . "')";
		$rsChk = $users->LoadRs($sFilter);
		if ($rsChk && !$rsChk->EOF) {
			$_SESSION[EW_SESSION_MESSAGE] = "Duplicate value for index or primary key -- `user_pass`, value = " . $users->user_pass->CurrentValue;
			$rsChk->Close();
			return FALSE;
		}
	}
	$rsnew = array();

	// Field fullname
	$users->fullname->SetDbValueDef($users->fullname->CurrentValue, NULL);
	$rsnew['fullname'] =& $users->fullname->DbValue;

	// Field user_name
	$users->user_name->SetDbValueDef($users->user_name->CurrentValue, NULL);
	$rsnew['user_name'] =& $users->user_name->DbValue;

	// Field user_pass
	$users->user_pass->SetDbValueDef($users->user_pass->CurrentValue, NULL);
	$rsnew['user_pass'] =& $users->user_pass->DbValue;

	// Field email
	$users->email->SetDbValueDef($users->email->CurrentValue, NULL);
	$rsnew['email'] =& $users->email->DbValue;

	// Field address
	$users->address->SetDbValueDef($users->address->CurrentValue, NULL);
	$rsnew['address'] =& $users->address->DbValue;

	// Field user_handphone
	$users->user_handphone->SetDbValueDef($users->user_handphone->CurrentValue, NULL);
	$rsnew['user_handphone'] =& $users->user_handphone->DbValue;

	// Field user_homephone
	$users->user_homephone->SetDbValueDef($users->user_homephone->CurrentValue, NULL);
	$rsnew['user_homephone'] =& $users->user_homephone->DbValue;

	// Field user_level
	if ($Security->CanAdmin()) { // System admin
	$users->user_level->SetDbValueDef($users->user_level->CurrentValue, 0);
	$rsnew['user_level'] =& $users->user_level->DbValue;
	}

	// Call Row Inserting event
	$bInsertRow = $users->Row_Inserting($rsnew);
	if ($bInsertRow) {
		$conn->raiseErrorFn = 'ew_ErrorFn';
		$AddRow = $conn->Execute($users->InsertSQL($rsnew));
		$conn->raiseErrorFn = '';
	} else {
		if ($users->CancelMessage <> "") {
			$_SESSION[EW_SESSION_MESSAGE] = $users->CancelMessage;
			$users->CancelMessage = "";
		} else {
			$_SESSION[EW_SESSION_MESSAGE] = "Insert cancelled";
		}
		$AddRow = FALSE;
	}
	if ($AddRow) {
		$users->id->setDbValue($conn->Insert_ID());
		$rsnew['id'] =& $users->id->DbValue;

		// Call Row Inserted event
		$users->Row_Inserted($rsnew);
	}
	return $AddRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
